package com.wxproject.demo.Controller;

import com.wxproject.demo.Common.lang.ResultBody;
import com.wxproject.demo.Entity.AO.UserInfoAlterAO;
import com.wxproject.demo.Entity.VO.AllUserInfo;
import com.wxproject.demo.Entity.VO.UserInfoVO;
import com.wxproject.demo.Service.UserInfoService;
import com.wxproject.demo.Utils.JWTUtil;
import com.wxproject.demo.Utils.RedisUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zqf
 * @date 2021/4/25 21:07
 * @description
 */
@RestController
@RequestMapping("/user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @PostMapping("/alterUserInfo")
    public ResultBody alterUserInfo(@RequestBody UserInfoAlterAO userInfoAlterAO, HttpServletRequest rq){
        String id = JWTUtil.getUserId(rq.getHeader("Token"));
        userInfoAlterAO.setId(id);
        Integer res = userInfoService.alterUserInfo(userInfoAlterAO);
        if (res!=0){
            return ResultBody.ok().message("修改成功");
        }
        return ResultBody.error().message("修改失败");
    }

    @GetMapping("/searchUserInfo/{id}")
    public ResultBody searchUserInfo(@PathVariable("id")String id){
        AllUserInfo userInfo = userInfoService.getAllUserInfo(id);
        return ResultBody.ok().data("userInfo",userInfo);
    }

    @GetMapping("/putUserInfoOnRedis")
    public ResultBody putUserInfoOnRedis(){
        List<AllUserInfo> allUserInfo = userInfoService.getAllUser();
        for (int i = 0; i <allUserInfo.size() ; i++) {
            Map<String, Object> infoMap = new HashMap<>();
            infoMap.put("id",allUserInfo.get(i).getId());
            infoMap.put("nickname",allUserInfo.get(i).getNickname());
            infoMap.put("avatarUrl",allUserInfo.get(i).getAvatarUrl());
            RedisUtil.hmset("userInfo-"+allUserInfo.get(i).getUsername(),infoMap);
        }
//        RedisUtil.hmset();
        return null;
    }
}
